/* MarkusCrawler
 *
 * Copyright (C) 2011 by Markus '[mackan]' Hyttinen <mackanhyttinen@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */


package me.crawler;

import java.util.ArrayList;
import java.util.regex.Pattern;

public class CrawlerFilter {
	
	
	
	protected ArrayList<String> delimiters = new ArrayList<String>();
	
	
	 /**
	 * @return the delimiters
	 */
	public synchronized ArrayList<String> getDelimiters() {
		return delimiters;
	}


	/**
	 * @param delimiters the delimiters to set
	 */
	public synchronized void setDelimiters(ArrayList<String> delimiters) {
		this.delimiters = delimiters;
	}

	/**
	 * @param delimiter to add to delimiters
	 */
	public synchronized void addDelimiter(String delimiter) {
		this.delimiters.add(delimiter);
	}

	Pattern filters = Pattern.compile(".*(\\.(css|js|bmp|gif|jpeg"
	            + "|png|tiff?|mid|mp2|mp3|mp4|svg|jpg"
	            + "|wav|avi|mov|mpeg|ram|m4v|pdf"
	            + "|rm|smil|wmv|swf|wma|zip|rar|gz))$");
	
	
    public synchronized boolean validUrl(String url){
		
    	if(url == null)
    		return false;
    	if(!delimiters.isEmpty()){
    	boolean found  = false;
    	for (String del : delimiters){
    		if (url.startsWith(del))
    			found =  true;
    	}
    	if (!found)
    		return false;
    	}
    		
    	
        String link = url.toLowerCase();
        if (filters.matcher(link).matches()) 
                return false;
        
        if (link.contains("javascript:"))
            return false;

        if (link.startsWith("wiki"))
            return true;
        
        
        if (!link.contains("?")) 
            return true;
        
        return false;
    }
    
    
    
    public CrawlerFilter clone(){
    	
    	CrawlerFilter toReturn = new CrawlerFilter();
    	
    	toReturn.setDelimiters(delimiters);
    	
    	return toReturn;
    }
    

}
